<template>
	<view class="content">
		盲手道
		登陆后可享受会员权益
		<button type="primary" @click="getuserinfo">一键登录</button>
	</view>

</template>

<script>
	var app = getApp()
	export default {
		data() {
			return {
				title: 'Hello',
				tabbar: [],
				type: 99,
			}
		},
		onLoad() {
		},

		methods: {
			//获取用户信息
			getuserinfo() {
				var that = this
				uni.getUserProfile({
					desc: '登录',
					lang:'zh_CN',
					success: (res) => {
						console.log(res);
						uni.login({
							provider: 'weixin',
							success: function(loginRes) {
								console.log(loginRes.code);
								var data = {
									"avatar": res.userInfo.avatarUrl,
									"city": res.userInfo.city,
									"country": res.userInfo.country,
									"nickName": res.userInfo.nickName,
									"province": res.userInfo.province,
									"sex": res.userInfo.gender
								}
								console.log('输入', data)
								var http = '/api/user/wechat/login?' + 'code=' + loginRes
									.code
								that.$http(http, 'POST', data).then((res) => {
									console.log('授权登录', res.data.data.role)
									uni.setStorageSync('token',res.data.data.token)
									uni.setStorageSync('user',res.data.data.user)
									app.globalData.token = res.data.data.token
									app.globalData.user = res.data.data.user
									if(res.data.data.role==1){
										//用户
										uni.reLaunch({
										    url: '/pagesA/index/index'
										});
									}else if(res.data.data.role==2){
										//技师
										uni.reLaunch({
										    url: '/pagesB/index/index'
										});
									}else if(res.data.data.role==3){
										//门店
										uni.reLaunch({
										    url: '/pagesC/index/index'
										});
									}
									
								})
							}
						});
					},
					fail: (res) => {
						console.log("///???",res)
						uni.showToast({
							title:"您已取消授权",
							icon:'none'
						})
					}
				});
			},

			// getPhoneNumber(e){
			// 	console.log(e.detail.errMsg)
			// 	    console.log(e.detail.iv)
			// 	    console.log(e.detail.encryptedData)
			// }
		}
	}
</script>

<style lang="scss">
	.content {
		padding: 50rpx;
		text-align: center;

		button {
			margin: 30rpx 0;
		}
	}
</style>
